<?php
class ControllerModuleTreodenProductstabs extends Controller {
	protected function TreodenGetTopRatingProducts($limit){
		$product_data = array();
		$this->load->model('catalog/product');
		$query = $this->db->query("SELECT p.`product_id`, AVG(r.`rating`) AS aggrating, COUNT(r.`rating`) AS totalreviews FROM `" . DB_PREFIX . "review` r, `" . DB_PREFIX . "product` p  WHERE r.`status` = 1 AND p.`product_id` = r.`product_id` AND p.`status` = 1 GROUP BY r.`product_id` ORDER BY aggrating DESC, totalreviews DESC LIMIT " . (int)$limit);
		foreach ($query->rows as $result) { 		
			$product_data[$result['product_id']] = $this->model_catalog_product->getProduct($result['product_id']);
		}
					 	 		
		return $product_data;
	}
	protected function index($setting) {
		$this->load->model('tool/image');
		$this->load->model('catalog/product');
		$this->data['button_cart'] = $this->language->get('button_cart');
		if(!isset($setting['product_data'])) echo "There is no tab to display.Please add some tabs.";
		else{
			$tab = array();
			foreach($setting['product_data'] as  $key=>$value){
				$tab[$key]['title'] = $setting['title'][$key];
				if (empty($setting['limit'][$key])) {
					$setting['limit'][$key] = 10;
				}
				switch ($value) {
					case 'bestsellers':
						$results = $this->model_catalog_product->getBestSellerProducts($setting['limit'][$key]);
						foreach ($results as $result) {
							if ($result['image']) {
								$image = $this->model_tool_image->resize($result['image'], $setting['width'][$key], $setting['height'][$key]);
							} else {
								$image = false;
							}
							
							if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
								$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
							} else {
								$price = false;
							}
									
							if ((float)$result['special']) {
								$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
							} else {
								$special = false;
							}	
							
							if ($this->config->get('config_review_status')) {
								$rating = $result['rating'];
							} else {
								$rating = false;
							}
											
							$tab[$key]['products'][] = array(
								'product_id' => $result['product_id'],
								'thumb'   	 => $image,
								'name'    	 => $result['name'],
								'price'   	 => $price,
								'special' 	 => $special,
								'rating'     => $rating,
								'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
								'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
							);
						}
						break;
					case 'featured':
						$products = explode(',', $this->config->get('featured_product'));
						$products = array_slice($products, 0, (int)$setting['limit'][$key]);
						foreach ($products as $product_id) {
							$product_info = $this->model_catalog_product->getProduct($product_id);
							
							if ($product_info) {
								if ($product_info['image']) {
									$image = $this->model_tool_image->resize($product_info['image'], $setting['width'][$key], $setting['height'][$key]);
								} else {
									$image = false;
								}

								if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
									$price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
								} else {
									$price = false;
								}
										
								if ((float)$product_info['special']) {
									$special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
								} else {
									$special = false;
								}
								
								if ($this->config->get('config_review_status')) {
									$rating = $product_info['rating'];
								} else {
									$rating = false;
								}
									
								$tab[$key]['products'][] = array(
									'product_id' => $product_info['product_id'],
									'thumb'   	 => $image,
									'name'    	 => $product_info['name'],
									'price'   	 => $price,
									'special' 	 => $special,
									'rating'     => $rating,
									'reviews'    => sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']),
									'href'    	 => $this->url->link('product/product', 'product_id=' . $product_info['product_id'])
								);
							}
						}
						break;
					case 'latest':
						$data = array(
							'sort'  => 'p.date_added',
							'order' => 'DESC',
							'start' => 0,
							'limit' => $setting['limit'][$key]
						);

						$results = $this->model_catalog_product->getProducts($data);

						foreach ($results as $result) {
							if ($result['image']) {
								$image = $this->model_tool_image->resize($result['image'], $setting['width'][$key], $setting['height'][$key]);
							} else {
								$image = false;
							}
										
							if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
								$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
							} else {
								$price = false;
							}
									
							if ((float)$result['special']) {
								$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
							} else {
								$special = false;
							}
							
							if ($this->config->get('config_review_status')) {
								$rating = $result['rating'];
							} else {
								$rating = false;
							}
							
							$tab[$key]['products'][] = array(
								'product_id' => $result['product_id'],
								'thumb'   	 => $image,
								'name'    	 => $result['name'],
								'price'   	 => $price,
								'special' 	 => $special,
								'rating'     => $rating,
								'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
								'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
							);
						}
						break;
					case 'specials':
						$data = array(
							'sort'  => 'pd.name',
							'order' => 'ASC',
							'start' => 0,
							'limit' => $setting['limit'][$key]
						);

						$results = $this->model_catalog_product->getProductSpecials($data);

						foreach ($results as $result) {
							if ($result['image']) {
								$image = $this->model_tool_image->resize($result['image'], $setting['width'][$key], $setting['height'][$key]);
							} else {
								$image = false;
							}

							if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
								$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
							} else {
								$price = false;
							}
									
							if ((float)$result['special']) { 
								$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
							} else {
								$special = false;
							}
							
							if ($this->config->get('config_review_status')) {
								$rating = $result['rating'];
							} else {
								$rating = false;
							}
							
							$tab[$key]['products'][] = array(
								'product_id' => $result['product_id'],
								'thumb'   	 => $image,
								'name'    	 => $result['name'],
								'price'   	 => $price,
								'special' 	 => $special,
								'rating'     => $rating,
								'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
								'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id'])
							);
						}
						break;
					case 'most-popular':
						$results = $this->model_catalog_product->getPopularProducts($setting['limit'][$key]);
						foreach ($results as $result) {
							if ($result['image']) {
								$image = $this->model_tool_image->resize($result['image'], $setting['width'][$key], $setting['height'][$key]);
							} else {
								$image = false;
							}
							
							if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
								$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
							} else {
								$price = false;
							}
									
							if ((float)$result['special']) {
								$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
							} else {
								$special = false;
							}	
							
							if ($this->config->get('config_review_status')) {
								$rating = $result['rating'];
							} else {
								$rating = false;
							}
											
							$tab[$key]['products'][] = array(
								'product_id' => $result['product_id'],
								'thumb'   	 => $image,
								'name'    	 => $result['name'],
								'price'   	 => $price,
								'special' 	 => $special,
								'rating'     => $rating,
								'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
								'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
							);
						}
						break;
					case 'top-rating':
						$data = array(
							'sort'  => 'rating',
							'order' => 'DESC',
							'start' => 0,
							'limit' => $setting['limit'][$key]
						);

						$results = $this->model_catalog_product->getProducts($data);
						foreach ($results as $result) {
							if ($result['image']) {
								$image = $this->model_tool_image->resize($result['image'], $setting['width'][$key], $setting['height'][$key]);
							} else {
								$image = false;
							}
							
							if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
								$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
							} else {
								$price = false;
							}
									
							if ((float)$result['special']) {
								$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
							} else {
								$special = false;
							}	
							
							if ($this->config->get('config_review_status')) {
								$rating = $result['rating'];
							} else {
								$rating = false;
							}
											
							$tab[$key]['products'][] = array(
								'product_id' => $result['product_id'],
								'thumb'   	 => $image,
								'name'    	 => $result['name'],
								'price'   	 => $price,
								'special' 	 => $special,
								'rating'     => $rating,
								'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
								'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
							);
						}
						break;	
				}
			}
			$this->data['tabs'] = $tab;
			$this->data['columns'] = $setting['columns'];
			$this->document->addScript('catalog/view/javascript/jquery/jquery.isotope.min.js');
			$this->document->addStyle('catalog/view/javascript/jquery/style.css');
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/treoden_productstabs.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/module/treoden_productstabs.tpl';
			} else {
				$this->template = 'default/template/module/treoden_productstabs.tpl';
			}
			
			$this->render();
		}
	}
}
?>